import PureServiceDetail from '../components/service-detail';
import {fetchServiceBySearch, getServiceById, updateServiceTag, updatePermission} from 'actions/service';
import { getFetchStatus } from 'actions/util';
import * as React from 'react';
import * as ReactRedux from 'react-redux';

const mapStateToProps = (state) => {
  return {
    isFetching: getFetchStatus(state, getServiceById),
    service: state.serviceById
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    initFetch: (serviceId) => {
      dispatch(getServiceById(serviceId))
    },
    onServiceTagSave: (serviceId, tags) => {
      return dispatch(updateServiceTag(serviceId, tags))
        .then(()=>dispatch(getServiceById(serviceId)));
    },
    onRemoveServiceTag: (serviceId, tags) => {
      return dispatch(updateServiceTag(serviceId, tags))
        .then(()=>dispatch(getServiceById(serviceId)));
    },
    onPermissionSave: (serviceId, allow, denial) => {
      return dispatch(updatePermission(serviceId, allow, denial))
        .then(()=>dispatch(getServiceById(serviceId)));
    }
  }
};

class ServiceDetail extends React.Component {

  componentDidMount() {
    this.props.initFetch(this.props.id);
  }

  render() {
    return <PureServiceDetail {...this.props}/>;
  }
}

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(ServiceDetail);
